using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;
using Services;
using MainGame.GameSound;

namespace MainGame.GamePlay
{
  /// <summary>
  /// Gestiamo la logica del giocatore, ascoltando l'evento di fine turno e
  /// i clicks su una carta
  /// </summary>
  public abstract class PlayerLayer : GamePlayLayer
  {

    public PlayerLayer(Game game, BoardState state)
      : base(game, state)
    {
      UpdateOrder = (int)ComponentOrders.LOGIC;
    }

    public override void Initialize()
    {
      Game.GetBoardRenderer().ReleasePlayerCard += new ReleasePlayerCard(BoardManager_ReleasePlayerCard);

      base.Initialize();
    }

    protected override void BoardManager_ReleasePlayerCard(int player_index, Card c)
    {
      //Combination validCombination = ComputeValidCombination(c, selectedCards);
      //if (validCombination.cardsIndices.Any())
      //{
      //  List<Card> combinationCards = new List<Card>();
      //  // Converto la lista di indici in una lista di carte
      //  for (int i = 0; i < validCombination.cardsIndices.Count; i++)
      //    combinationCards.Add(blocks.ElementAt(0).Cards[validCombination.cardsIndices.ElementAt(i)]);

        //playCardOnTable(c, combinationCards);
      //}
      //else
      //{
        //new Thread(() =>
        //{
        //  //Animazione
        //  players[player_index].MossaFatta = true;
        //  animations.Add(new CardAnimation() { Card = c, AnimationType = AnimationType.MoveTo, PlayerIndex = current_player_index, AnimationFinished = false });

        //  Thread.Sleep(100);
        //  while (players[current_player_index].MossaFatta) { Thread.Sleep(10); }
        //  while (!players[current_player_index].MossaAnimata) { Thread.Sleep(10); }
        //}).Start();

        blocks.ElementAt(0).Cards.Add(c);
        players[player_index].Cards.Remove(c);

        TurnOver();
      //}
    }
  }
}
